package com.example.demo;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.LinkedList;
import java.util.List;

public class PhantomReferenceTest {
    private static final List<Object> LIST = new LinkedList<>();
    private static final ReferenceQueue<M> QUEUE = new ReferenceQueue();
    public static void main(String[] args) {
        PhantomReference<M> phantomReference = new PhantomReference<>(new M(),QUEUE);
        new Thread(new Runnable() {
            @Override
            public void run() {
               while (true){
                   //1M
                   LIST.add(new byte[1024*1024]);
                   try {
                       Thread.sleep(1000L);
                   }catch (Exception e){
                       e.printStackTrace();
                   }
                   System.out.println(phantomReference.get());
                   
               }
            }
        }).start();
    }
}
